package com.manager.controller;

import com.manager.bean.PageInfo;
import com.manager.bean.TimingSQL;
import com.manager.service.TimingSQLService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class TimingSqlController {

    @Resource
    private TimingSQLService timingSQLService;

    @RequestMapping(value = "/findTimingSheet")
    public String findTimingSheet(Model model,String name, Integer pageIndex, Integer pageSize) {
        PageInfo<TimingSQL> pi = timingSQLService.findPageInfo(name,pageIndex,pageSize);
        model.addAttribute("pi", pi);
        return "timgingsheet_list";
    }


    @RequestMapping(value = "/exportTimingSheetlist", method = RequestMethod.POST)
    @ResponseBody
    public List<TimingSQL> exportTimingSheetlist() {
        List<TimingSQL> timingSQLS = timingSQLService.getAll();
        return timingSQLS;
    }


    @RequestMapping("/deleteTimingSheet")
    @ResponseBody
    public String deleteTimingSheet(Integer Id) {
        timingSQLService.deleteById(Id);
        return "timgingsheet_list";
    }


    @RequestMapping(value = "/addTimingSheet", method = RequestMethod.POST)
    @ResponseBody
    public String addTimingSheet(@RequestBody TimingSQL timingSQL) {
        timingSQLService.insert(timingSQL);
        return "timgingsheet_list";
    }


    @RequestMapping(value = "/updateTimingSheet", method = RequestMethod.POST)
    public String updateDormitory(TimingSQL timingSQL) {
        timingSQLService.update(timingSQL);
        return "redirect:/findTimingSheet";
    }


    @RequestMapping("/findTimingSheetById")
    public String findDormitoryById(Integer Id, HttpSession session, Model model) {
        TimingSQL s = timingSQLService.queryById(Id);
        session.setAttribute("s", s);
        model.addAttribute("s", s);
        return "timgingsheet_list";
    }
}
